<?php
namespace app\massage\model;

use app\BaseModel;
use app\member\model\Level;
use app\member\model\Rights;
use longbingcore\wxcore\PospalApi;
use think\facade\Db;

class BalanceOrder extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_balance_order_list';

//
    protected $append = [

        'nick_name',


    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-11 15:47
     * @功能说明:用户昵称
     */
    public function getNickNameAttr($value,$data){

        if(!empty($data['user_id'])){

            $user_model = new User();

            return $user_model->where(['id'=>$data['user_id']])->value('nickName');

        }

    }













    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['status'] = 1;

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-22 11:31
     * @功能说明:订单支付回调
     */
    public function orderResult($order_code,$transaction_id){

        $order = $this->dataInfo(['order_code'=>$order_code]);

        if(!empty($order)&&$order['status']==1){

            $user_model = new User();

            $water_model= new BalanceWater();

            $user = $user_model->dataInfo(['id'=>$order['user_id']]);

            Db::startTrans();

            $update = [

                'transaction_id' => $transaction_id,

                'status'         => 2,

                'pay_time'       => time(),

                'now_balance'   => $order['true_price']+$user['balance']

            ];
            //修改订单信息
            $res = $this->dataUpdate(['id'=>$order['id']],$update);

            if($res==0){

                Db::rollback();

            }

            $update = [

                'balance' => $order['true_price']+$user['balance'],

                'member_level' => $order['member_level'],

                'vip_time'     => $user['vip_time']==0?time():0

            ];
            //修改用户余额
            $res = $user_model->dataUpdate(['id'=>$order['user_id']],$update);

            if($res==0){

                Db::rollback();

            }
            //添加余额流水
            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'price'   => $order['true_price'],

                'order_id'=> $order['id'],

                'add'     => 1,

                'type'    => 1,

                'before_balance' => $user['balance'],

                'after_balance'  => $order['true_price']+$user['balance'],
            ];

            $res = $water_model->dataAdd($insert);

            if($res==0){

                Db::rollback();

            }
            //给会员权益
            if(!empty($order['member_level'])){

                $rights_model = new Rights();

                $rights_model->giveRights($order['member_level'],$order['user_id'],$order['id']);

            }

            Db::commit();
            //向银豹同步会员
            if(!empty($order['member_level'])){

                $level_model = new Level();

                $api_model   = new PospalApi();

                $level    = $level_model->levelInfo(['id'=>$order['member_level']]);

                $nickName = $user_model->where(['id'=>$order['user_id']])->value('nickName');

                $insert = [

                    'categoryName' => $level['title'],

                    'discount'     => $level['discount'],

                    'number'       => time().rand(1,100),

                    'name'         => !empty($nickName)?$nickName:$order['phone'],

                    'phone'        => $order['phone'],

                ];

                $res = $api_model->addMember($insert);

                if($res['status']!='success'){
                    //修改订单信息
                    $this->dataUpdate(['id'=>$order['id']],['error_msg'=>$res['messages'][0]]);
                }else{

                    $this->dataUpdate(['id'=>$order['id']],['have_syn'=>1]);

                }

            }

        }

        return true;

    }





}